import 'package:async/async.dart';
import 'package:boss/main.dart';
import 'package:boss/models/account_for_time.dart';
import 'package:boss/models/commodity_ranking_page_data.dart';
import 'package:boss/ui/widget/calendar/util/date_util.dart';
import 'package:boss/ui/widget/loader/loader_pull_refresh.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:flustars/flustars.dart' as flustars;
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';

class CommodityRankingType {
  //今天
  static const int TODAY = 0;

  //本周
  static const int WEEK = 1;

  //本月
  static const int MONTH = 2;
}

class AccountProvider extends ChangeNotifier {
  int page = 1;

  List<CommodityRanking> commodityRankings;

  int commodityRankingType = CommodityRankingType.TODAY;

  String get startTime {
    var now = DateTime.now();

    if (commodityRankingType == CommodityRankingType.TODAY) {
      return flustars.DateUtil.formatDate(
        DateUtils.startOfTheDay(now),
        format: flustars.DateFormats.full,
      );
    } else if (commodityRankingType == CommodityRankingType.WEEK) {
      return flustars.DateUtil.formatDate(
        DateUtils.startOfTheWeek(now),
        format: flustars.DateFormats.full,
      );
    } else {
      return flustars.DateUtil.formatDate(
        DateUtils.startOfTheMonth(now),
        format: flustars.DateFormats.full,
      );
    }
  }

  String get endTime {
    var now = DateTime.now();
    if (commodityRankingType == CommodityRankingType.TODAY) {
      return flustars.DateUtil.formatDate(
        DateUtils.endOfTheDay(now),
        format: flustars.DateFormats.full,
      );
    } else if (commodityRankingType == CommodityRankingType.WEEK) {
      return flustars.DateUtil.formatDate(
        DateUtils.endOfTheWeek(now),
        format: flustars.DateFormats.full,
      );
    } else {
      return flustars.DateUtil.formatDate(
        DateUtils.endOfTheMonth(now),
        format: flustars.DateFormats.full,
      );
    }
  }

  List<StoreIncomeForTime> storeIncomeForTime;

  final GlobalKey<LoaderPullRefreshState> loaderPullRefreshKey = GlobalKey<LoaderPullRefreshState>();

  Future<Result<List<StoreIncomeForTime>>> initAccountForTime() async {
    try {
      var response = await api.queryAccountItemsForTime();
      if (!response.successful()) {
        return Result.error(response.msg);
      }

      storeIncomeForTime = response.data..sort((a, b) => a.type - b.type);
      notifyListeners();

      return Result.value(storeIncomeForTime);
    } catch (e) {
      return Result.error(e.toString());
    }
  }

  Future<Result<List<CommodityRanking>>> initData() async {
    var accountForTime = await initAccountForTime();
    var commodityRankings = await refreshCommodityRankingList();

    return accountForTime.isValue && commodityRankings.isValue
        ? commodityRankings
        : accountForTime.isError
            ? accountForTime
            : commodityRankings;
  }

  firstInitData() async {
    if (storeIncomeForTime == null || commodityRankings == null) loaderPullRefreshKey.currentState.initData();
  }

  Future<Result<List<CommodityRanking>>> queryCommodityRankingList() async {
    try {
      var response = await api.queryAndSortCommodityCellDate(
        page: page,
        startTime: startTime,
        endTime: endTime,
      );
      if (!response.successful()) {
        return Result.error(response.msg);
      }

      page == 1 ? commodityRankings = response.data.items : commodityRankings.addAll(response.data.items);

      notifyListeners();

      return Result.value(response.data.items);
    } catch (e) {
      return Result.error(e.toString());
    }
  }

  Future<Result<List<CommodityRanking>>> refreshCommodityRankingList() async {
    page = 1;
    return queryCommodityRankingList();
  }

  Future<Result<List<CommodityRanking>>> loadMoreCommodityRankingList() async {
    page += 1;
    return queryCommodityRankingList();
  }

  changeCommodityRankingType(int type) async {
    commodityRankingType = type;
    notifyListeners();
    loaderPullRefreshKey.currentState.refresh(needMove: false);
  }

  setTargetAccount(StoreIncomeForTime storeIncomeForTime, int targetAccount) async {
    try {
      showLoadingToast();

      var result = await api.setStoreTargetAccount(targetAccount, storeIncomeForTime.type);

      if (!result.successful()) {
        toast(result.msg);
        return;
      }

      storeIncomeForTime.targetAmount = targetAccount;
      notifyListeners();

      toast('设置目标营业额成功');
    } catch (e) {
      toast(e.toString());
    } finally {
      hideLoadingToast();
    }
  }

  clearData() {
    page = 1;

    commodityRankings = null;

    commodityRankingType = CommodityRankingType.TODAY;
  }
}
